// miniprogram/pages/mynote/visitors/visitors.js
Page({

  data: {
    // 页码数
    pageIndex: 0,
    hasData: true, //是否有更多数据
    overView: true, //是否在全览状态下
    moreLoading: true, //是否正在加载更多
    //访客区域
    visitorPlaces: [],
    //访客集合
    markers: [],
    inPoints: [],
    selVisitorId: '',
    visitorList: [],
    //地图配置
    mapSetting: {
      skew: 0,
      rotate: 0,
      latitude: '35.603719',
      longitude: '106.875000',
      showLocation: true,
      showScale: false,
      subKey: '',
      layerStyle: -1,
      enableZoom: true,
      enableScroll: true,
      enableRotate: false,
      showCompass: false,
      enable3D: false,
      enableOverlooking: false,
      enableSatellite: false,
      enableTraffic: false,
    }
  },
  onLoad: function (options) {
    this.setData({
      onling: getApp().globalData.onling
    })
    if (this.data.onling)
      return
    this.getPageVisitors()
    this.getVisitorPlace()
  },
  // 获取游客区域数据
  getVisitorPlace() {
    wx.cloud.callFunction({
      name: 'database',
      data: {
        ctName: 'visitor',
        sortByCount: '$addressInfo.ad_info.city'
      }
    }).then(res => {
      console.log("getVisitorPlace", res)
      let visitorPlaces = res.result.list
      this.setData({
        visitorPlaces
      })
    }).catch(err => {
      console.log("getVisitorPlace", err)
    })
  },
  // 获取分页访客数据
  getPageVisitors() {
    let skipCount = this.data.pageIndex * 10
    wx.cloud.callFunction({
      name: 'database',
      data: {
        ctName: 'visitor',
        sortObj: {
          skip: skipCount,
          limit: 10,
          sort: {
            dateTime: -1
          }
        }
      }
    }).then(res => {
      console.log("getPageVisitors", res)
      this.configMapMarkers(res.result.list);
    }).catch(err => {
      console.log("getPageVisitors", err)
    })
  },
  // 配置地图maker数据
  configMapMarkers(visitors) {
    let markers = []
    let inPoints = []
    let hasData = visitors.length > 0
    let pageIndex = this.data.pageIndex + 1
    let mapSetting = this.data.mapSetting
    // 缓存的visitorList数据
    let mVisitorList = this.data.visitorList
    let visitorList = mVisitorList.concat(visitors)
    visitorList.forEach((item, index) => {
      if (item.addressInfo.location) {
        let location = item.addressInfo.location
        let iconFlag = index % 4
        // 配置marker信息
        let marker = {
          width: 25,
          height: 25
        }
        marker.id = index
        marker.latitude = location.lat
        marker.longitude = location.lng
        marker.iconPath = `/images/ic_user${iconFlag}.png`
        markers.push(marker)
        // 配置point信息
        let point = {
          latitude: location.lat,
          longitude: location.lng
        }
        inPoints.push(point)
      }
    })
    mapSetting.scale = 3
    mapSetting.latitude = '35.603719'
    mapSetting.longitude = '106.875000'
    mapSetting.markers = markers
    mapSetting.includePoints = inPoints
    this.setData({
      markers,
      inPoints,
      visitorList,
      mapSetting,
      pageIndex,
      hasData,
      overView: true,
      moreLoading: false,
      selVisitorId: ''
    })
  },
  // 划动到底部监听
  toBottomListener() {
    if (!this.data.hasData) {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none'
      })
      return
    }
    this.setData({
      moreLoading: true
    })
    setTimeout(() => {
      this.getPageVisitors()
    }, 1000)
  },
  // 用户点击监听
  onUserItemClick(e) {
    console.log("onUserItemClick", e)
    let selVisitorId = e.currentTarget.dataset.id
    let location = e.currentTarget.dataset.location
    if (location === undefined)
      return
    let mapSetting = this.data.mapSetting
    let mMarker = {
      width: 30,
      height: 30,
      latitude: location.lat,
      longitude: location.lng,
      iconPath: '/images/ic_user2.png'
    }
    mapSetting.scale = 10
    mapSetting.latitude = location.lat
    mapSetting.longitude = location.lng
    mapSetting.markers = [mMarker]
    mapSetting.includePoints = []
    this.setData({
      mapSetting,
      selVisitorId,
      overView: false
    })
  },
  // 全览按钮点击监听
  onOverViewClick(e) {
    console.log("onOverViewClick", e)
    let mapSetting = this.data.mapSetting
    mapSetting.scale = 3
    mapSetting.latitude = '35.603719'
    mapSetting.longitude = '106.875000'
    mapSetting.markers = this.data.markers
    mapSetting.includePoints = this.data.inPoints
    this.setData({
      mapSetting,
      overView: true,
      selVisitorId: ''
    })
  }
})